package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.service.IHostService;
import com.j48.entrancegurad.service.IUserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

@Controller
public class UserController {
    @Autowired
    private IHostService hostService;
    @Autowired
    private IUserService userService;

    /**
     * 通过用户姓名查找用户
     * @param req
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/selectuser")
    @ResponseBody
    public ModelAndView selectUserByName(HttpServletRequest req, User user,ModelAndView model){
        String username=req.getParameter("userName");
        user.setUser_name(username);
        ArrayList<User> userlist=new ArrayList<User>();
        if(!username.equals("")){
        userlist = userService.selectByName(user);
        }else{
            userlist=userService.selectAll();
        }
        ArrayList<Integer> userId=new ArrayList<Integer>();
        ArrayList<Host> hostlist=new ArrayList<Host>();
        Host host=new Host();
        for(int i=0;i<userlist.size();i++){
            userId.add(i,userlist.get(i).getUser_host_id());
        }
        for(int i=0;i<userId.size();i++){
            host.setHost_id(userId.get(i));
            hostlist.add(i,hostService.selectById(host));
        }
        req.getSession().setAttribute("hostlistall",hostlist);
        req.getSession().setAttribute("userlist",userlist);
        model=new ModelAndView("jsp/user");
        return model;
    }

    /**
     * 通过业主名查询用户
     * @param req
     * @param model
     * @param host
     * @param user
     * @return
     */
    @RequestMapping("/selecthost")
    @ResponseBody
    public ModelAndView selecthostname(HttpServletRequest req,ModelAndView model,Host host,User user){
        String hostname=req.getParameter("userHost");
        host=new Host();
        user=new User();
        ArrayList<Host> hostlist=new ArrayList<Host>();
        ArrayList<Host> hostlist1=new ArrayList<Host>();
        ArrayList<User> userlist=new ArrayList<User>();
        ArrayList<User> userlistsmall=new ArrayList<User>();
        if(hostname.equals("")){
            hostlist=hostService.selectAll();
            for(int i=0;i<hostlist.size();i++){
                user.setUser_host_id(hostlist.get(i).getHost_id());
                userlistsmall=userService.selectByHostId(user);
                for(int k=0;k<userlistsmall.size();k++){
                    userlist.add(userlistsmall.get(k));
                    hostlist1.add(hostlist.get(i));
                }
            }
        }else{
        host.setHost_name(hostname);
        hostlist=hostService.selectByName(host);
        for(int i=0;i<hostlist.size();i++){
         user.setUser_host_id(hostlist.get(i).getHost_id());
         userlistsmall=userService.selectByHostId(user);
         for(int k=0;k<userlistsmall.size();k++){
         userlist.add(userlistsmall.get(k));
         hostlist1.add(hostlist.get(i));
         }
        }
        }
        req.getSession().setAttribute("userlist",userlist);
        req.getSession().setAttribute("hostlistall",hostlist1);
        model=new ModelAndView("jsp/user");
        return model;
    }
}
